package net.wanho.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.wanho.dto.BuyListDTO;
import net.wanho.entity.BuyList;
import net.wanho.entity.InStore;
import net.wanho.mapper.BuyListMapper;
import net.wanho.mapper.InStoreMapper;
import net.wanho.service.BuyListService;
import net.wanho.vo.BuyListVo;
import net.wanho.vo.PageVo;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
* @author Administrator
* @description 针对表【buy_list(采购单表)】的数据库操作Service实现
* @createDate 2025-05-05 02:08:25
*/
@Service
@RequiredArgsConstructor
public class BuyListServiceImpl extends ServiceImpl<BuyListMapper, BuyList> implements BuyListService{
    private final BuyListMapper buyListMapper;
    private final InStoreMapper inStoreMapper;

    @Override
    public PageVo<BuyList> getListPage(BuyListDTO buyListDTO, Integer pageNum, Integer pageSize) {
        IPage<BuyList> page = new Page<>(pageNum, pageSize);
        QueryWrapper<BuyList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(buyListDTO.getStoreId()), "b.store_id", buyListDTO.getStoreId())
                .like(ObjectUtil.isNotEmpty(buyListDTO.getProductName()), "p.product_name", buyListDTO.getProductName())
                .like(ObjectUtil.isNotEmpty(buyListDTO.getBuyUser()), "b.buy_user", buyListDTO.getBuyUser())
                .eq(Objects.nonNull(buyListDTO.getIsIn()), "b.is_in", buyListDTO.getIsIn())
                .lt(ObjectUtil.isNotEmpty(buyListDTO.getEndTime()), "b.buy_time", buyListDTO.getEndTime())
                .gt(ObjectUtil.isNotEmpty(buyListDTO.getStartTime()), "b.buy_time", buyListDTO.getStartTime());

        List<BuyList> buyLists = baseMapper.selectBuyListPage(page, queryWrapper);

        return new PageVo<>(
                page.getCurrent(),
                page.getSize(),
                page.getTotal(),
                page.getPages(),
                page.offset(),
                buyLists
        );
    }

    @Override
    public void insertBuyList(BuyListVo buyListVo) {
        if(Objects.nonNull(buyListVo.getSuppDate())
            && buyListVo.getSuppDate().isBefore(LocalDate.now())){
            throw new RuntimeException("供应商日期已过期");
        }
        BuyList buyList = new BuyList();
        BeanUtil.copyProperties(buyListVo, buyList);
        buyList.setIsIn("0");
        buyListMapper.insert(buyList);
    }

    @Override
    public void updatePurchaseById(BuyListVo buyListVo) {
        BuyList buyList = new BuyList();
        BeanUtil.copyProperties(buyListVo, buyList);
        if(Objects.equals(buyListVo.getIsIn(), "0")) {
            buyList.setBuyTime(LocalDateTime.now());
        } else {
            InStore inStore = new InStore(
                    buyList.getStoreId(),
                    buyList.getProductId(),
                    buyList.getFactBuyNum(),
                    "0"
            );
            inStoreMapper.insert(inStore);
        }
        buyListMapper.updateById(buyList);
    }
}




